/**
 * 宝宝活动管理
 * 
 * @return {}
 */
var queryActivityBabySearch = function() {
	var queryActivityBabyFieldSet = new Ext.form.FieldSet({
				id : 'queryActivityBabyFieldSet',
				title : '查询条件',
				collapsible : true,
				buttonAlign : 'center',
				keys : [{
							key : 13, // 13代表回车
							fn : queryActivityBaby,
							scope : this
						}],
				items : [{
					layout : 'column',
					border : false,
					items : [{
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
											xtype : 'textfield',
											anchor : '100%',
											fieldLabel : '内容',
											name : "content"
										}]
							}, {
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
									xtype : 'localcommonbox',
									anchor : '100%',
									fieldLabel : '性别',
									editable : false,
									hiddenName : "sex",
									loaclarr : [['0', '男孩'], ['1', '女孩'],
											['', '全部']],
									value : ''

								}]
							}, {
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
									xtype : 'localcommonbox',
									anchor : '100%',
									fieldLabel : '可用状态',
									editable : false,
									hiddenName : "isUsable",
									loaclarr : [['0', '可用'], ['1', '不可用'],
											['', '全部']],
									value : ''

								}]
							}, {
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
									layout : 'column',
									border : false,
									items : [{
												text : '查询',
												xtype : 'button',
												width : 80,
												handler : function() {
													queryActivityBaby();
												},
												style : {
													marginLeft : "100px",
													marginRight : "10px"
												}
											}, {
												text : '重置',
												xtype : 'button',
												width : 80,
												handler : function() {
													queryActivityBabyForm
															.getForm().reset();
												}
											}]
								}]
							}]
				}]
			});
	fieldSetListeners(['queryActivityBabyFieldSet', 'queryActivityBabyForm',
					'ActivityBabyManagerGrid'], [0.15, 0.85]);

	var queryActivityBabyForm = new Ext.form.FormPanel({
				id : 'queryActivityBabyForm',
				anchor : '100% 15%',
				frame : true,
				labelAlign : 'right',
				bodyBorder : false,
				items : [queryActivityBabyFieldSet]
			});
	return queryActivityBabyForm;
};

/**
 * grids
 */
var ActivityBabyManagerGrid = function() {
	var ActivityBabyManagerCm = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(), {
				dataIndex : "id",
				header : "编号",
				sortable : true,
				width : 60
			}, {
				dataIndex : "sex",
				header : "性别",
				sortable : true,
				width : 80,
				renderer : function(value) {
					return PmsUtil.activityBabySex(value);;
				}
			}, {
				dataIndex : "content",
				header : "内容",
				sortable : true,
				width : 600
			}, {
				dataIndex : "isUsable",
				header : "可用状态",
				sortable : true,
				width : 70,
				renderer : function(value) {
					return PmsUtil.activityBabyIsUsable(value);
				}
			}, {
				dataIndex : "createdName",
				header : "创建人",
				sortable : true,
				width : 100
			}, {
				dataIndex : "createdDate",
				header : "创建时间",
				sortable : true,
				width : 130
			}, {
				header : "管理",
				width : 100,
				dataIndex : "id",
				sortable : false,
				renderer : function(j, i, g, m, h) {
					var l = g.data.id;
					var isUsable = g.data.isUsable;
					var k = "";
					if (isUsable == 0 && isGranted("activityBabyManager_修改")) {
						k += '&nbsp;&nbsp;<a href="#" onclick="updateActivityBaby('
								+ l + ');" >修改</a>';
					}
					if (isUsable == 0 && isGranted("activityBabyManager_禁用")) {
						k += '&nbsp;&nbsp;<a href="#" onclick="updActivityBabyStatus('
								+ l + ',' + "1" + ',' + "'禁用'" + ')\" >禁用</a>';
					}
					if (isUsable == 1 && isGranted("activityBabyManager_启用")) {
						k += '&nbsp;&nbsp;<a href="#" onclick="updActivityBabyStatus('
								+ l + ',' + "0" + ',' + "'启用'" + ')\" >启用</a>';

					}
					return k;
				}
			}]);

	// 定义分析器
	var advManagerReader = new Ext.data.JsonReader({
				totalProperty : "totalCount",
				root : "list",
				fields : [{
							name : "id"
						}, {
							name : "sex"
						}, {
							name : "content"
						}, {
							name : "isUsable"
						}, {
							name : "createdDate"
						}, {
							name : "createdName"
						}]
			});

	// 取数据store
	var adervtViewStore = new Ext.data.Store({
				autoLoad : true,// 要加载数据必须设置为true
				proxy : new Ext.data.HttpProxy({
							url : __ctxPath
									+ "/activityManager/selectAllActivityBabys"
						}),
				remoteSort : true,
				baseParams : {
					start : 0,
					limit : 25
				},
				listeners : {
					beforeload : {
						fn : function(thiz, options) {
							Ext.apply(thiz.baseParams, Ext
											.getCmp('queryActivityBabyForm')
											.getForm().getValues());
						}
					}
				},
				reader : advManagerReader
			});
	// 定义grid的toolbar
	var advManagerTb = new Ext.Toolbar({
				id : "advManagerTb",
				height : 25,
				items : []
			});

	advManagerTb.add(new Ext.scm.Button({
				text : "添加",
				iconCls : "btn-add",
				width : 80,
				hsValue : "activityBabyManager_添加",
				pressed : true,
				listeners : {
					"click" : function() {
						addActivityBaby();
					}
				}
			}));
	advManagerTb.add(new Ext.Toolbar.Spacer());
	advManagerTb.add(new Ext.Button({
				text : "刷 新",
				iconCls : "btn-refresh",
				width : 80,
				pressed : true,
				handler : function() {
					queryActivityBaby();
				}
			}));
	var ActivityBabyManagerGrid = new Ext.grid.EditorGridPanel({
				id : 'ActivityBabyManagerGrid',
				store : adervtViewStore,
				cm : ActivityBabyManagerCm,
				autoScroll : true,
				clicksToEdit : 1,
				loadMask : true,
				trackMouseOver : true,
				stripeRows : true,
				columnLines : true,
				loadMask : {
					msg : "正在加载数据，请稍侯……"
				},
				viewConfig : {
					emptyText : '没有您想要的数据!'
				},
				anchor : '100% 85%',
				frame : true,
				tbar : advManagerTb,
				bbar : new PG.PagingBar({
							id : 'adervtGridPage',
							store : adervtViewStore
						})
			});
	return ActivityBabyManagerGrid;
};

// 查询
function queryActivityBaby() {
	var grid = Ext.getCmp("ActivityBabyManagerGrid");
	var store = grid.store;
	var pagesize = Ext.getCmp('adervtGridPage').pageSize;
	store.load({
				params : {
					start : 0,
					limit : pagesize
				}
			});
}

// 添加宝宝内容信息
function addActivityBaby() {
	try {
		addOrUpdateActivityBaby(null, "添加内容").show();
	} catch (Error) {
		Ext.Loader.load([__ctxPath + '/js/avtivity/activityBabyForm.js'],
				function() {
					addOrUpdateActivityBaby(null, "添加内容").show();
				});
	}
}

// 修改宝宝内容信息
function updateActivityBaby(id) {
	try {
		addOrUpdateActivityBaby(id, "修改内容").show();
	} catch (Error) {
		Ext.Loader.load([__ctxPath + '/js/avtivity/activityBabyForm.js'],
				function() {
					addOrUpdateActivityBaby(id, "修改内容").show();
				});
	}
}

// 修改状态
function updActivityBabyStatus(id, status, message) {
	Ext.Msg.confirm("信息确认", "是否" + message + "该条内容信息?", function(btn) {
				if (btn == "yes") {
					var optionMask = new Ext.LoadMask(Ext.getBody(), {
								msg : "正在处理,请稍后..."
							});
					optionMask.show();
					Ext.Ajax.request({
								url : __ctxPath
										+ "/activityManager/updateAdvIsUsableById",
								params : {
									id : id,
									isUsable : status
								},
								method : "post",
								success : function(response) {
									optionMask.hide();
									var jsonData = Ext.util.JSON
											.decode(response.responseText);
									if (jsonData.success) {
										Ext.MessageBox.show({
													title : "信息提示",
													msg : jsonData.data,
													buttons : Ext.MessageBox.OK,
													icon : Ext.MessageBox.INFO
												});
										queryActivityBaby();
									} else {
										Ext.MessageBox.show({
													title : "错误提示",
													msg : jsonData.data,
													buttons : Ext.MessageBox.OK,
													icon : Ext.MessageBox.ERROR
												});
									}
								},
								failure : function(response) {
									optionMask.hide();
									Ext.MessageBox.show({
												title : "操作信息",
												msg : "服务器请求异常,请稍后再试!",
												buttons : Ext.MessageBox.OK,
												icon : Ext.MessageBox.ERROR
											});
								}
							});
				}
			});
}


Ext.getCmp("centerTabPanel").getActiveTab().add(queryActivityBabySearch(),
		ActivityBabyManagerGrid());
Ext.getCmp("centerTabPanel").getActiveTab().doLayout();
